﻿using System;
using System.Data.Entity;
using System.Data.Linq;
using System.Linq;
using System.Linq.Expressions;

namespace ELTE.SEM.Data.Repository
{
    public class Repository<T> : IRepository<T> where T : class
    {
        protected DbContext _context;
        protected DbSet<T> DbSet;

        public Repository(DbContext context)
        {
            _context = context;
            DbSet = context.Set<T>();
        }

        public bool Insert(T entity)
        {
            try
            {
                DbSet.Add(entity);
                return true;
            }
            catch (Exception ex)
            {
                //TODO: log error
                return false;
            }
            
        }

        public IQueryable<T> GetAll()
        {
            try
            {
                return DbSet;
            }
            catch (Exception ex)
            {
                //TODO: log error
                return null;
            }
        }

        public T GetById(int Id)
        {
            return DbSet.Find(Id);
        }


        public void Save()
        {
            try
            {
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                //TODO: log error
            }
            
        }


        public bool Delete(T entity)
        {
            try
            {
                DbSet.Remove(entity);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            
        }
    }
}
